package com.itany.corejava.day17;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class TestServerB {

    public static void main(String[] args) {
        ServerSocket ss=null;
        try{

            ss=new ServerSocket(8888);

            while(true){
                Socket s=null;

                BufferedReader br=null;

                PrintWriter pw=null;

                try{

                    s=ss.accept();

                    br=new BufferedReader(
                            new InputStreamReader(
                                    s.getInputStream()
                            )
                    );

                    String name=br.readLine();

                    if("abc".equals(name)) {
                        Thread.sleep(999999);
                    }

                    pw=new PrintWriter(s.getOutputStream());

                    pw.println("Hello "+name);

                    pw.flush();

                }catch (Exception e){
                    e.printStackTrace();
                }finally{
                    if(br!=null){
                        br.close();
                    }
                    if(pw!=null){
                        pw.close();
                    }
                    if(s!=null){
                        s.close();
                    }
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(ss!=null){
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
